package com.pingxx.boot.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pingxx.boot.demo.entity.User;
import com.pingxx.boot.demo.mapper.UserMapper;
import com.pingxx.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = "userCache")
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User insert(User user) {
        userMapper.insert(user);
        return user;
    }

    @CacheEvict(key = "#id")
    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Cacheable(key = "#id")
    @Override
    public User selectById(Long id) {
        return userMapper.selectById(id);
    }

    @CachePut(key = "#user.id")
    @Override
    public User update(User user) {
        userMapper.updateById(user);
        return user;
    }

    @Override
    public IPage<User> selectAll(long current, long size) {
        IPage<User> userPage = new Page<>(current, size);
        userPage = userMapper.selectPage(userPage, null);
        return userPage;
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectList(null);
    }
}
